package com.baoyouqun.service.impl;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baoyouqun.base.utils.ConvertUtils;
import com.baoyouqun.domain.CreateCmd.VipLevelCreateCmd;
import com.baoyouqun.domain.Query.VipLevelQuery;
import com.baoyouqun.domain.UpdateCmd.VipLevelUpdateCmd;
import com.baoyouqun.domain.VO.VipLevelVO;
import com.baoyouqun.entity.VipLevel;
import com.baoyouqun.mapper.VipLevelMapper;
import com.baoyouqun.service.VipLevelService;
import jakarta.annotation.Resource;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class VipLevelServiceImpl extends ServiceImpl<VipLevelMapper, VipLevel> implements VipLevelService {

    @Resource
    private VipLevelMapper vipLevelMapper;

    @Override
    public SingleResponse<VipLevelVO> create(VipLevelCreateCmd cmd) {
        VipLevel entity =
                ConvertUtils.copyBean(cmd, VipLevel.class);
        Boolean result = save(entity);
        if (!result) {
            return SingleResponse.buildFailure("100101", "创建失败");
        }

        VipLevelVO vo =
                ConvertUtils.copyBean(entity, VipLevelVO.class);
        return SingleResponse.of(vo);
    }

    @Override
    public Response update(VipLevelUpdateCmd cmd) {
        if (Objects.isNull(cmd)) {
            return Response.buildFailure("100301", "参数不能为空");
        }
        if (StringUtil.isBlank(cmd.getId())) {
            return Response.buildFailure("100302", "ID不能为空");
        }
        VipLevel oldEntity = getById(cmd.getId());
        if (Objects.isNull(oldEntity)) {
            return Response.buildFailure("100303", "未查询到此数据");
        }
        oldEntity = ConvertUtils.copyBean(cmd, VipLevel.class);
        Boolean result = updateById(oldEntity);
        if (result) {
            return Response.buildSuccess();
        } else {
            return Response.buildFailure("100304", "更新失败");
        }
    }

    @Override
    public VipLevelVO selectById(String id) {
        VipLevel entity = vipLevelMapper.selectById(id);
        VipLevelVO vo =
                ConvertUtils.copyBean(entity, VipLevelVO.class);
        return vo;
    }

    @Override
    public IPage<VipLevelVO> pageVo(VipLevelQuery query) {
        IPage<VipLevel> page = new Page<VipLevel>(query.getCurrent(), query.getSize());
        page = page(page, new LambdaQueryWrapper<VipLevel>());
        Page<VipLevelVO> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<VipLevelVO> records = ConvertUtils.copyBeanList(page.getRecords(), VipLevelVO.class);
        voPage.setRecords(records);
        return voPage;
    }
}